package com.technician.config;

import com.technician.exceptionHandler.AccessDeniedHandlerImpl;
import com.technician.exceptionHandler.AuthenticationEntryPointImpl;
import com.technician.filter.JwtAuthenticationTokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private AuthenticationEntryPointImpl authenticationEntryPoint;

    @Autowired
    private AccessDeniedHandlerImpl accessDeniedHandler;

    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                // 关闭 csrf ,不然 Security会发送 一个 token 去校验是不是跨站攻击,前后端分离没有这个问题
                .csrf().disable()
                // 不从 session 中获取 SecurityContext
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                // 登录接口可以匿名访问
                .antMatchers("/user/login").anonymous()
                .antMatchers("/user/register").anonymous()
                .antMatchers("/wxLogin").anonymous()
                // 放行接口文档
                .antMatchers("/doc.html").anonymous()
                .antMatchers("/v2/api-docs-ext/**").anonymous()
                .antMatchers("/v2/api-docs").anonymous()
                .antMatchers("/webjars/**").anonymous()
                .antMatchers("/favicon.ico").anonymous()
                .antMatchers("/swagger-resources").anonymous()
                // 其他请求需要认证
                .anyRequest().authenticated();

        // 配置异常处理器
        http.exceptionHandling()
                // 配置授权失败处理器
                .accessDeniedHandler(accessDeniedHandler)
                // 配置认证失败处理器
                .authenticationEntryPoint(authenticationEntryPoint);

        // 在 UsernamePasswordAuthenticationFilter 过滤器之前添加 jwtAuthenticationTokenFilter
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);

        // 允许跨域
        http.cors();
    }

    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    // 创建 BCryptPasswordEncoder 注入容器
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
}